package com.sgcc.statistics.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sgcc.statistics.domain.OServiceIndex;
import com.sgcc.statistics.mapper.OServiceIndexMapper;
import com.sgcc.statistics.service.IOServiceIndexService;
import com.sgcc.statistics.utils.PercentUtils;
import org.activiti.engine.impl.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.statistics.mapper.ODigitalScaleMapper;
import com.sgcc.statistics.domain.ODigitalScale;
import com.sgcc.statistics.service.IODigitalScaleService;

/**
 * 数字化服务客户应用规模Service业务层处理
 * 
 * @author sgcc
 * @date 2021-03-28
 */
@Service
public class ODigitalScaleServiceImpl implements IODigitalScaleService 
{
    @Autowired
    private ODigitalScaleMapper oDigitalScaleMapper;
    @Autowired
    private OServiceIndexMapper oServiceIndexMapper;

    /**
     * 查询数字化服务客户应用规模
     * 
     * @param id 数字化服务客户应用规模ID
     * @return 数字化服务客户应用规模
     */
    @Override
    public ODigitalScale selectODigitalScaleById(String id)
    {
        return oDigitalScaleMapper.selectODigitalScaleById(id);
    }

    /**
     * 查询数字化服务客户应用规模列表
     * 
     * @param oDigitalScale 数字化服务客户应用规模
     * @return 数字化服务客户应用规模
     */
    @Override
    public List<ODigitalScale> selectODigitalScaleList(ODigitalScale oDigitalScale)
    {

//        oDigitalScale.setAuthNo(UserUtils.getUser().getCompany().getCode());
//		if(oDigitalScale.getOrgNo() != null && oDigitalScale.getOrgNo().equals("35101")){
//			//查全部市
//			oDigitalScale.setOrgNo("");
//		}
//        List<String> orgNos = officeService.getOrgNos(oDigitalScale.getOrgNo(),oDigitalScale.isCounty());
//        if(CollectionUtil.isEmpty(orgNos)){
//            return new ArrayList<>();
//        }
        OServiceIndex index1 = oServiceIndexMapper.selectByCode("0101");
        OServiceIndex index2 = oServiceIndexMapper.selectByCode("0102");
        OServiceIndex index3 = oServiceIndexMapper.selectByCode("0103");
//        List<ODigitalScale> totals = oDigitalScaleMapper.selectTotal(oDigitalScale);
        List<ODigitalScale> oDigitalScaleList = oDigitalScaleMapper.selectODigitalScaleList(oDigitalScale);
        if(CollectionUtil.isNotEmpty(oDigitalScaleList)){
            for(ODigitalScale scale:oDigitalScaleList){
                scale.setKeepRateStr(PercentUtils.getPercentNum(scale.getKeepRate()));
                scale.setKeepGrowStr(PercentUtils.getPercentNum(scale.getKeepGrow()));
                scale.setActiveRateStr(PercentUtils.getPercentNum(scale.getActiveRate()));
                scale.setActiveGrowStr(PercentUtils.getPercentNum(scale.getActiveGrow()));
                scale.setCertRateStr(PercentUtils.getPercentNum(scale.getCertRate()));
                scale.setCertGrowStr(PercentUtils.getPercentNum(scale.getCertGrow()));
                //留存指标完成率
                if(scale.getKeepRate()!=null){
                    scale.setKeepCompRateStr(PercentUtils.getPercentNum(scale.getKeepRate().divide(index1.getTarget(),4,4)));
                }
                //活跃指标完成率
                if(scale.getActiveRate()!=null){
                    scale.setActiveCompRateStr(PercentUtils.getPercentNum(scale.getActiveRate().divide(index2.getTarget(),4,4)));
                }
                //实名认证指标完成率
                if(scale.getCertRate()!=null){
                    scale.setCertCompRateStr(PercentUtils.getPercentNum(scale.getCertRate().divide(index3.getTarget(),4,4)));
                }
            }
//            result.setList(oDigitalScaleList);
////			result.setCount(oDigitalScaleList.size());
//            //合计
//            if(CollectionUtil.isNotEmpty(totals)){
//                ODigitalScale total = totals.get(0);
//                if(total!=null){
//                    total.setKeepRateStr(PercentUtils.getPercentNum(total.getKeepRate()));
//                    total.setKeepGrowStr(PercentUtils.getPercentNum(total.getKeepGrow()));
//                    total.setActiveRateStr(PercentUtils.getPercentNum(total.getActiveRate()));
//                    total.setActiveGrowStr(PercentUtils.getPercentNum(total.getActiveGrow()));
//                    total.setCertRateStr(PercentUtils.getPercentNum(total.getCertRate()));
//                    total.setCertGrowStr(PercentUtils.getPercentNum(total.getCertGrow()));
//                    //留存指标完成率
//                    total.setKeepCompRateStr(PercentUtils.getPercentNum(total.getKeepRate().divide(index1.getTarget(),4,4)));
//                    //活跃指标完成率
//                    total.setActiveCompRateStr(PercentUtils.getPercentNum(total.getActiveRate().divide(index2.getTarget(),4,4)));
//                    //实名认证指标完成率
//                    total.setCertCompRateStr(PercentUtils.getPercentNum(total.getCertRate().divide(index3.getTarget(),4,4)));
//                }
//                result.setTotalEntity(total);
//            }
        }

        return oDigitalScaleList;
    }

    /**
     * 新增数字化服务客户应用规模
     * 
     * @param oDigitalScale 数字化服务客户应用规模
     * @return 结果
     */
    @Override
    public int insertODigitalScale(ODigitalScale oDigitalScale)
    {
        return oDigitalScaleMapper.insertODigitalScale(oDigitalScale);
    }

    /**
     * 修改数字化服务客户应用规模
     * 
     * @param oDigitalScale 数字化服务客户应用规模
     * @return 结果
     */
    @Override
    public int updateODigitalScale(ODigitalScale oDigitalScale)
    {
        return oDigitalScaleMapper.updateODigitalScale(oDigitalScale);
    }

    /**
     * 批量删除数字化服务客户应用规模
     * 
     * @param ids 需要删除的数字化服务客户应用规模ID
     * @return 结果
     */
    @Override
    public int deleteODigitalScaleByIds(String[] ids)
    {
        return oDigitalScaleMapper.deleteODigitalScaleByIds(ids);
    }

    /**
     * 删除数字化服务客户应用规模信息
     * 
     * @param id 数字化服务客户应用规模ID
     * @return 结果
     */
    @Override
    public int deleteODigitalScaleById(String id)
    {
        return oDigitalScaleMapper.deleteODigitalScaleById(id);
    }
}
